< previous page page_290 next page >

Page 290
(table continued from previous page)
BIT #2BIT #2BIT #1BIT #0VALUE
10106
10115
11004
11013
11102
11111

A nibble can show up to 16 different values, but the meanings of those values depend on how the computer language chooses to store data. When the highest bit (the one with the highest position number) is used to indicate negative numbers, the variable is considered a signed variable.1 Otherwise, the variable is considered unsigned. Visual Basic programmers rarely worry about this, because most Visual Basic variable types are signed. But API and DLL functions can use unsigned variables.
Let's say a DLL function has a parameter that takes an unsigned nibble, and you want to pass the number 14 to it. If you have only a signed nibble variable, you could pass it the value 2. Look at the table: 14 unsigned is identical to 2 signed!
Of course, neither DLLs nor the API use nibbles. We only use them here to save us the trouble of looking at larger tables. But this is a very important reason. In fact, before we look at larger numbers of bits, let's agree that from here on we'll deal with them in groups of 4 bits. If we want to work with 16 bits, instead of 0000000000000000 for 16 bits of 0, we'll just use 0000, where each digit represents a nibble. If we want to represent binary 0011 0011 0011 0011, we'll use 3333, since each 0011 has an unsigned value of 3.
But what if we want to deal with the 16-bit binary number 1010 1011 1100 1101? The first nibble is 10, the second 11, the third 12, and the fourth 13. We could write 10111213, but that would be very confusingit's much easier if each nibble has a single digit. This is fine from 0 to 9, but for higher numbers we need to use letters instead, as follows:
BIT #3BIT #2BIT #1BIT #0HEX DIGIT
00000
00011
00102
00113

(table continued on next page)
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
1 It's easy to see why the values 07 were assigned to the eight positive numbers, but why do the negative numbers count down from8 to1? This is not an accident, but the explanation is unfortunately beyond the scope of this book. The signed format used here is called 2's complement. In this format you can negate a number by complementing it (flipping the value of each of the number's bits) then adding one. This format avoids having a separate positive and negative zero value and makes arithmetic operations with negative and positive numbers work correctly.

 
< previous page page_290 next page >